package com.infore.common.utils;

import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.function.Function;

/**
 * @Discription TODO
 * @Autor lijianhua
 * @Date 2020/3/4 17:27
 */
@Component
public class CacheMap<T,R> extends HashMap {

    private T params;

    public CacheMap<T,R> setParams(T params){
        this.params = params;
        return this;
    }

    public R get(Object key, Function<? super T, ? extends R> fn) {
        Object object = super.get(key);
        if(object == null){
            object = (R)fn.apply(params);
            super.put(key,object);
        }
        return (R)object;
    }
}
